<?php
/*
	[Bidcms.com!] (C)2009-2011 Bidcms.com.
	This is NOT a freeware, use is subject to license terms
	$author limengqi
	$Id: userclass.php 2016-03-24 10:42 $
*/
if(!defined('IN_BIDCMS')) {
	exit('Access Denied');
}
class order_base_class extends model
{
	public $table='order';
	
	
	public $definition=array(
		'primary'=>'id'
	);
    public function callback($pay_type,$res){
      global $base;
      if(empty($res)){
        return array('errcode'=>-1,'errmsg'=>'返回值为空','errinfo'=>'failed');
      }

      $file =ROOT_PATH.'models/order/pay/'.strtolower($pay_type).'.php';

      if(file_exists($file)){
        include_once($file);
        $class = new $pay_type();
        
        $request = $class->getRule($res);
        if(!empty($request['order_no'])){
          $order_info = $this->get_one(array('and order_sn=:no',array(':no'=>$request['order_no'])));
          if($order_info['pay_status'] == 1){
            return array('errcode'=>0,'errmsg'=>'','errinfo'=>$class->success);
          }
          if(empty($order_info)){
            return array('errcode'=>-1,'errmsg'=>'订单不存在','errinfo'=>$class->error);
          }
          if($order_info['payable'] !== $request['amount']){
            return array('errcode'=>-1,'errmsg'=>'支付金额不符','errinfo'=>$class->error);
          }
          if(!empty($order_info) && $order_info['payment_id'] > 0){
            $payment = $base->bidcms_model('payment_base')->get_info_primary_id($order_info['payment_id']);
            $config = array(
              'merchant_id' => $payment['merchant_id'],
              'merchant_key' => $payment['merchant_key']
            );
            $class->setConfig($config);
            
            $verify = $class->verify($request['result']);
            if($verify){
              $this->update_data(array('and id=:id',array(':id'=>$order_info['id'])),array(
                'pay_status'=>1,
                'paid'=>$request['amount'],
                'pay_time'=>time(),
                'pay_id'=>$request['trade_no'],
                'pay_mchid'=>$config['merchant_id'],
                'pay_appid'=>''
              ));
              $base->bidcms_model('order_commission')->update_data(array('and order_id=:id',array(':id'=>$order_info['id'])),array('order_status'=>1));
              return array('errcode'=>0,'errmsg'=>'','errinfo'=>$class->success);
            }
          }
        }
      }
      
    }
    public function pay($order_info,$ext_config = array()){
      global $base;
      
      if(is_int($order_info)){
        $order_info = $this->get_info_primary_id(intval($order_info));
      } elseif(is_string($order_info)){
        $order_info = $this->get_one(array('and order_sn=:no',array(':no'=>$order_info)));
      }
      
      if(empty($order_info)){
        return '{"errcode":-1,"errmsg":"订单不存在"}';
      }
      if($order_info['pay_status'] != 0){
         return '{"errcode":-1,"errmsg":"订单已经支付了"}';
      }
      if(empty($order_info['payment_id'])){
         return '{"errcode":-1,"errmsg":"支付方式为空"}';
      }
      $order_goods = $base->bidcms_model('order_goods')->fields('goods_name,sku_name')->get_one(array(' and order_id=:oid',array(':oid'=>$order_info['id'])));
      
      $payment = $base->bidcms_model('payment_base')->get_info_primary_id(intval($order_info['payment_id']));
      if($payment &&  $payment['platform_id']>0){
          $platform = $base->bidcms_model('payment_platform')->get_info_primary_id($payment['platform_id']);

          $file =ROOT_PATH.'models/order/pay/'.strtolower($platform['code']).'.php';
          if(file_exists($file)){
            include_once($file);
            $type = $platform['code'];
            $config = array(
              'order_no' => $order_info['order_sn'], //订单号
              'total_fee' => $order_info['payable'], //金额
              'point' => $order_info['goods_point'], //积分
              'customer_id' => $order_info['customer_id'],
              'notify_url' => 'http:'.SITE_ROOT.'index.php?con=callback&act=notify&type='.$platform['code'],
              'return_url' => 'http:'.SITE_ROOT.'/index.php?con=callback',
              'merchant_id' => $payment['merchant_id'],
              'merchant_key' => $payment['merchant_key'],
              'shop_id' => $order_info['shop_id'],
              'gateway' => $payment['gateway'],
              'payment' => $payment['pay_type'],
              'content' => $order_goods['goods_name'].$order_goods['sku_name']
            );
            if(!empty($ext_config)){
            	$config = array_merge($config,$ext_config);
            }
            $class = new $type($config);
            if($class != false){
                return $class->submit();
            }
          }
      }
      return '{"errcode":-1,"errmsg":"发起支付失败"}';
    }
}
